<template>
    <div class="page_1">
        <div class="top_box">
            <el-button type="warning" icon="el-icon-plus" size="small" @click="addPrize">添加奖品</el-button>
        </div>
        <!-- 表格 -->
        <el-table :data="data" style="width: 100%" border>

            <el-table-column prop="prize" label="奖项">
            </el-table-column>
            <el-table-column prop="v" label="概率">
            </el-table-column>
            <el-table-column label="奖品图片">
                <template slot-scope="{row}">
                    <el-image style="width:60px;height:60px" :src="row.img" :preview-src-list="[row.img]">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column prop="desc" label="奖品描述">
            </el-table-column>
            <el-table-column prop="sort" label="排序">
            </el-table-column>
            <el-table-column align="center" label="操作" width="300">
                <template slot-scope="{ row }">
                    <el-button type="warning" @click="edit(row)">编辑</el-button>
                </template>
            </el-table-column>

        </el-table>

        <!-- 表单 -->
        <el-dialog :visible.sync="dialogVisible" :title="formTitle" append-to-body width="900px" @close="formReset">
            <el-form ref="form" :rules="rules" :model="form" label-width="80px">
                <el-row type="flex" :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="奖品名称" prop="prize">
                            <el-input v-model="form.prize" placeholder="请设置奖品名称"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="概率" prop="v">
                            <el-input v-model="form.v" placeholder="请设置概率"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="排序" prop="sort">
                            <el-input v-model="form.sort" placeholder="请输入排序"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="描述" prop="desc">
                            <el-input v-model="form.desc" placeholder="请输入描述"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="奖品图片" prop="img">
                            <el-upload-image v-model="form.img"></el-upload-image>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" justify="center">
                    <el-button type="primary" @click="initiate()">发布</el-button>
                    <el-button @click="formReset()" type="primary">重置</el-button>
                </el-row>
            </el-form>
        </el-dialog>

        <!-- 分页 -->
        <el-pagination class="main-pagination" :total="total" :current-page.sync="page" :page-size="limit" layout="prev, pager, next, jumper,total" @current-change="getInfo" hide-on-single-page></el-pagination>
    </div>
</template>

<script>
import {
    get, del, post
} from "@/assets/js/http.js";
export default {
    components: {
        "el-upload-image": () => import("@/components/el-upload-image.vue")
    },
    data() {
        return {
            formTitle: "添加奖品",
            total: 0,
            limit: 20,
            page: 1,
            // 表格数据
            data: [],
            dialogVisible: false,
            form: {
                prize: "",
                v: "",
                sort: "",
                desc: "",
                img: []
            },
            rules: {
                prize: [
                    { required: true, message: '请输入奖品名称', trigger: 'blur' },
                ],
                v: [
                    { required: true, message: '请设置概率', trigger: 'blur' },
                ],
                sort: [
                    { required: true, message: '请输入排序数值', trigger: 'blur' },
                ],
                desc: [
                    { required: true, message: '请输入描述', trigger: 'blur' },
                ],
                img: [
                    { required: true, message: '请上传奖品图片', trigger: 'blur' },

                ],
                min: [
                    { required: true, message: '请输入最小话费金额', trigger: 'blur' },
                    { type: 'number', message: '金额必须为数字值' }
                ],
                max: [
                    { required: true, message: '请输入最小话费金额', trigger: 'blur' },
                    { type: 'number', message: '金额必须为数字值' }
                ]
            }
        }
    },
    methods: {
        // 表单初始数据
        getInfo(index = 1) {
            post({
                url: "admin/answer.Prize/index",
                data: {
                    page: index,
                    limit: this.limit,
                },
                loading: true,
            }).then(res => {
                this.total = res.total
                this.data = res.data;
            })
        },
        // 添加奖品
        addPrize() {
            this.formTitle = "添加奖品"
            this.dialogVisible = true
        },
        // 表单发布
        initiate() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    let data = JSON.parse(JSON.stringify(this.form))
                    data.img = data.img[0]?.url
                    post({
                        url: "admin/answer.Prize/edit",
                        data,
                        loading: true,
                        successTip: true,
                    }).finally(() => {
                        this.getInfo(this.page)
                        this.dialogVisible = false
                    })
                }
            });

        },
        // 编辑
        edit(row) {
            this.formTitle = "编辑奖品"
            let data = JSON.parse(JSON.stringify(row))
            data.img = [{
                name: "data,img",
                status: "success",
                uid: 1679368485028,
                url: data.img
            }]
            this.form = data
            this.dialogVisible = true
            console.log(this.form);
        },
        // 表单关闭回调
        formReset() {
            this.form = Object.assign({}, this.$options.data().form)
        },
    },
    mounted() {
        this.getInfo()
    },
}
</script>

<style></style>